<?php
namespace Admin\Controller;

use Think\Controller;

class OrderdetailsController extends RightController
{
    // 加载初始化内容页,并检索
    public function index()
    {
        //判断分页时搜索条件
        if (I("get.p")) {
            $where = session("QG_SEARCH_WHERE_orderdetails");
			$params = session("QG_SEARCH_PARAMS_orderdetails");
            $where = empty($where) ? "1" : $where;
            $pageSize = session("QG_SEARCH_SIZE_orderdetails");
        } else {
            //清除session存储
            session("QG_SEARCH_WHERE_orderdetails", null);
            session("QG_SEARCH_PARAMS_orderdetails", null);
            session("QG_SEARCH_SIZE_orderdetails", null);
            $pageSize = I("post.pageSize");
			$params = I("post.");

            $where = "1";
        }

        // POST数据提交，重新组合检索条件
        if (IS_POST) {
            unset($_GET["p"]);
            $where = $this->searchCondition();
        }
		
		// 分页输入
        if(empty($pageSize)){
            $pageSize = 10;
        }

        // 总条数
        $count = M('orderdetails')
            ->alias("t")
            ->where($where)
            ->count();
        $page = new \Admin\Model\Page($count, $pageSize);
        $show = $page->show();

        // 进行分页数据查询
        $list = M('orderdetails')
            ->alias("t")
            ->field("t.*")
            ->where($where)
            ->order('t.id DESC')
            ->limit($page->firstRow . ',' . $page->listRows)->select();
			
		// 统计
        $sum = M('orderdetails')
            ->alias("t")
            ->field("count(1) as countNum")
            ->where($where)
            ->order('t.id DESC')
            ->find();

        // 渲染查询条件到session
        session("QG_SEARCH_WHERE_orderdetails", $where);
		session("QG_SEARCH_PARAMS_orderdetails", $params);
		session("QG_SEARCH_SIZE_orderdetails", $pageSize);
		
		// 输出数据
        $this->assign("params", $params);
        $this->assign('list', $list);
        $this->assign('sum', $sum);
		$this->assign('lists', array(
            "page" => getDropdownList("pageSize" ,"" ,false)
        ));
        $this->assign('page', $show);
        $this->display();
    }

    // 搜索条件
    public function searchCondition()
    {
        $where = "1";

        // ID
        if (I('post.id')) {
            $where .= " AND t.id='" . I("post.id") . "'";
        }

        // 订单ID
        if (I('post.orderid')) {
            $where .= " AND t.orderid='" . I("post.orderid") . "'";
        }

        // 商品ID
        if (I('post.productid')) {
            $where .= " AND t.productid='" . I("post.productid") . "'";
        }

        // 数量
        if (I('post.num')) {
            $where .= " AND t.num='" . I("post.num") . "'";
        }

        // 价格
        if (I('post.priceStart') || I('post.priceEnd')) {
            // 开始
            if(I('post.priceStart')){
                $where .= " AND t.price>='" . I("post.priceStart") . "'";
            }
            // 结束
            if(I('post.priceEnd')){
                $where .= " AND t.price<='" . I("post.priceEnd") . "'";
            }
        }

        // 总价格
        if (I('post.totalStart') || I('post.totalEnd')) {
            // 开始
            if(I('post.totalStart')){
                $where .= " AND t.total>='" . I("post.totalStart") . "'";
            }
            // 结束
            if(I('post.totalEnd')){
                $where .= " AND t.total<='" . I("post.totalEnd") . "'";
            }
        }

        // 规格说明
        if (I('post.spec')) {
            // 精确
            if (I('post.specCondition') == 1) {
                $where .= " AND t.spec='" . I("post.spec") . "'";
            } else {
                $where .= " AND t.spec LIKE '%" . I("post.spec") . "%'";
            }
        }

        // 创建时间
        if (I('post.createtimeStart') || I('post.createtimeEnd')) {
            // 起始日期
            if(I('post.createtimeStart')){
                $where .= " AND t.createtime>='" . I("post.createtimeStart") . "'";
            }
            // 结束日期
            if(I('post.createtimeEnd')){
                $where .= " AND t.createtime<='" . I("post.createtimeEnd") . " 23:59:59'";
            }
        }

        // 更新时间
        if (I('post.updatetimeStart') || I('post.updatetimeEnd')) {
            // 起始日期
            if(I('post.updatetimeStart')){
                $where .= " AND t.updatetime>='" . I("post.updatetimeStart") . "'";
            }
            // 结束日期
            if(I('post.updatetimeEnd')){
                $where .= " AND t.updatetime<='" . I("post.updatetimeEnd") . " 23:59:59'";
            }
        }

        return $where;
    }

    // 添加会员
    public function add()
    {
        if (IS_POST) {
            // 数据
            $data = array();

                // ID
                    $data['id'] = I('post.id');
                // 订单ID
                    $data['orderId'] = I('post.orderid');
                // 商品ID
                    $data['productId'] = I('post.productid');
                // 数量
                    $data['num'] = I('post.num');
                // 价格
                    $data['price'] = I('post.price');
                // 总价格
                    $data['total'] = I('post.total');
                // 规格说明
                    $data['spec'] = I('post.spec');
                // 创建时间
                    $data['createTime'] = I('post.createtime');
                // 更新时间
                    $data['updateTime'] = I('post.updatetime');

            //创建数据
            $result = M('orderdetails')->add($data);

            if ($result) {
				$this->ajaxSuccess("添加成功！");
            } else {
				$this->ajaxError("添加失败！");
            }
        } else {
            $this->display();
        }
    }

    // 编辑资料
    public function edit()
    {
        if (IS_POST) {
            $return = array();

            // ID
            $id = I("post.id");
			
			// 没有ID
			if(empty($id)){
				$this->ajaxError("错误！");
			}

            // 判断用户是否存在
            $dbData = M('orderdetails')
                ->where(array("id", $id))
                ->find();

            //判断用户是否存在
            if (empty($dbData)) {
				$this->ajaxError("数据不存在！");
            }

            // 更新数据
            $data = array();

                // ID
                    $data['id'] = I('post.id');
                // 订单ID
                    $data['orderId'] = I('post.orderid');
                // 商品ID
                    $data['productId'] = I('post.productid');
                // 数量
                    $data['num'] = I('post.num');
                // 价格
                    $data['price'] = I('post.price');
                // 总价格
                    $data['total'] = I('post.total');
                // 规格说明
                    $data['spec'] = I('post.spec');
                // 创建时间
                    $data['createTime'] = I('post.createtime');
                // 更新时间
                    $data['updateTime'] = I('post.updatetime');

            // 更新数据
            $result = M('orderdetails')
                ->where(array("id" => $id))
                ->save($data);

            if ($result !== false) {
				$this->ajaxSuccess("更新成功！");
            } else {
				$this->ajaxError("更新失败！");
            }
        } else {
            // ID
            $id = intval(I("get.id"));

            // 判断是否存在
            $data = M('orderdetails')
                ->alias("t")
                ->field("t.*")
                ->where(array("t.id" => $id))
                ->find();

            // 判断是否存在
            if (empty($data)) {
                $this->error("数据不存在");
            }

            $this->assign("data", $data);
            $this->display();
        }
    }

    // 删除
    public function delete()
    {
        if (IS_POST) {
            // ID
            $id = intval(I("post.id"));
			
			// 没有ID
			if(empty($id)){
				$this->ajaxError("错误！");
			}

            //删除数据
            $result = M('orderdetails')->where(array("id" => $id))->delete();

            // 如果删除成功
            if ($result) {
				$this->ajaxSuccess("删除成功！");
            } else {
				$this->ajaxError("删除失败！");
            }
        }
    }
}